import { hashHistory } from 'react-router';

export default function request (method, url, body) {
  method = method.toUpperCase();
  if (method === 'GET') {
    body = undefined;
  } else {
    body = body && JSON.stringify(body);
  }

  return fetch(url, {
    method,
    headers: {

        'Access-Control-Allow-Origin':'*',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
         'Accept-Encoding':'gzip, deflate',
        'Connection':'keep-alive',
         'Accept':'*/*'
    },
    body
  })
    .then((res) => {
      // if (res.status === 401) {
      //   hashHistory.push('/login');
      //   return Promise.reject('Unauthorized.');
      // } else {
      //   const token = res.headers.get('access-token');
      //   if (token) {
      //     sessionStorage.setItem('access_token', token);
      //   }
        alert(res.toString())
        return res.json();
      // }
    });
}

export const gets = url => request('GET', url);
export const post = (url, body) => request('POST', url, body);
export const put = (url, body) => request('PUT', url, body);
export const del = (url, body) => request('DELETE', url, body);
